package singles;

import g4p_controls.G4P;
import g4p_controls.GButton;
import g4p_controls.GCScheme;
import g4p_controls.GEvent;
import g4p_controls.GTextArea;
import processing.core.PApplet;

import com.google.code.eventsonfire.EventHandler;
import com.google.code.eventsonfire.Events;


public class FireEvents extends PApplet  {

	MyEventReader eventReader;
	MyEventSender eventSender;
	
	public void setup() {
		size(278, 197, JAVA2D);
		createGUI();
		eventReader = new MyEventReader(textarea1);
		eventSender = new MyEventSender();
		
		//          from to
		Events.bind(this, this);
		Events.bind(this, eventReader);
		Events.bind(eventSender, eventReader);
	}

	public void draw() {
		background(230);
	}

	@EventHandler
	public void onMyEvent(MyEvent event) {
		textarea1.appendText("this << " + event.message);
	}
	
	static public class MyEventSender {

		public void sendAnEvent(){
			Events.fire(this, new MyEvent("@ " + System.currentTimeMillis()));	
		}
	}

	static public class MyEventReader {

		GTextArea logger;

		public MyEventReader(GTextArea txa) {
			logger = txa;
		}

		@EventHandler
		public void onEvent(MyEvent event) {
			logger.appendText(event.message);
		}
	}

	static public class MyEvent {
		public String message;

		public MyEvent(String m) {
			message = m;
		}
	}


	public void button1_click1(GButton source, GEvent event) {
		Events.fire(this, new MyEvent("1 - clicked"));
	}

	public void button2_click1(GButton source, GEvent event) {
		Events.fire(this, new MyEvent("2 - clicked"));
	} 

	public void button3_click1(GButton source, GEvent event) {
		eventSender.sendAnEvent();
	} 

	public void textchanged(GTextArea source, GEvent event) { //_CODE_:textarea1:925695:
		println("textarea1 - GTextArea event occured " + System.currentTimeMillis()%10000000 );
	} //_CODE_:textarea1:925695:



	// Create all the GUI controls. 
	// autogenerated do not edit
	public void createGUI(){
		G4P.messagesEnabled(false);
		G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
		G4P.setCursor(ARROW);
		if(frame != null)
			frame.setTitle("Sketch Window");
		button1 = new GButton(this, 15, 13, 80, 30);
		button1.setText("B1");
		button1.addEventHandler(this, "button1_click1");
		button2 = new GButton(this, 15, 54, 80, 30);
		button2.setText("B2");
		button2.addEventHandler(this, "button2_click1");
		button3 = new GButton(this, 14, 101, 80, 30);
		button3.setText("B3");
		button3.addEventHandler(this, "button3_click1");
		textarea1 = new GTextArea(this, 109, 7, 159, 177, G4P.SCROLLBARS_VERTICAL_ONLY);
		textarea1.setText("Log");
		textarea1.setOpaque(true);
		textarea1.addEventHandler(this, "textchanged");
	}

	// Variable declarations 
	// autogenerated do not edit
	GButton button1; 
	GButton button2; 
	GButton button3; 
	GTextArea textarea1; 
}
